<?php

/**
 * MangerService.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysmanger\Service;

class MangerService extends BaseService {
    
    public function delManger($mid)
    {
        if(!$mid)
        {
            return false;
        }
        
        $obj_mager_mdl = D('manager');
        $where['mid'] = $mid;
        return $obj_mager_mdl->where($where)->delete();
    }

    public function getManger($mid=null, $field='*')
    {
        $obj_mager_mdl = D('manager');
        $result = array();
        if ($mid)
        {
            if (is_array($mid))
            {
                $where['mid'] = array(
                        'in', $mid
                );
                $result = $obj_mager_mdl->where($where)->field($field)->select();
            }
        
            if (is_numeric($mid))
            {
                $where['mid'] = intval($mid);
                $result = $obj_mager_mdl->where($where)->field($field)->find();
            }
        }
        else
        {
            $result = $obj_mager_mdl->field($field)->select();
        }
        
        return $result;
    }
    // 保存数据
    public function saveManger($data)
    {
        if(!$data)
        {
            return false;
        }
        // 验证数据
        $this->_checkData($data);
        // 生成密码
        $this->createPwd($data['passwd'], $data['salt']);
        
        $obj_manger_mdl = D('manager');
        
        if($data['mid'])
        {
            $data['updated'] = NOW_TIME;
            $data['disable'] = 0;
            $mid = rintval($data['mid']);
            unset($data['mid']);
            $where['mid'] = $mid;
            
            return $obj_manger_mdl->where($where)->save($data);
        }
        
        $data['created'] = $data['updated'] = $data['login_time'] = NOW_TIME;
        
        return $obj_manger_mdl->data($data)->add();
    }
    
    protected function _checkData($data)
    {
        $errors = array();
        $options = array(
                'user' =>array(
                        'required' => array(
                            'rule'=>true,
                            'message'=>'用户名必填'
                        ),
                        'string_count_in_range' => array(
                                'rule'=>array(2,15),
                                'message'=>'用户名在2至15个长度之间'
                        ),
                ),
                'passwd' =>array(
                        'required' => array(
                                'rule'=>true,
                                'message'=>'密码必填'
                        ),
                ),
                'role_id' =>array(
                        'required' => array(
                                'rule'=>true,
                                'message'=>'请分配角色'
                        ),
                ),
        );
        
        $rs = \Base\Lib\Tools\Validator::make($errors, $data, $options);
        if(!$rs)
        {
            foreach ($errors as $k=>$v)
            {
                throw new \Exception($v);
            }
        }
        
        // 验证密码是否合法
        $this->verifyNewPwd($data['passwd']);
        
        return true;
    }
    
    // 获取管理员列表
    public function getMangerList($params)
    {

        if (! $params)
        {return false;}
        $obj_manger_mdl = D('manager');
        $count = $obj_manger_mdl->count();
        if (! $count)
        {return false;}
        $manger_list = $obj_manger_mdl->field($params['fields'])->order($params['order'])->limit($params['start'], $params['limit'])->select();
        if (! $manger_list)
        {return false;}
        $this->_formatList($manger_list);
        return array(
                    'count' => $count, 'list' => $manger_list 
        );
    }
    // 获取角色信息
    public function getRole($role_id = null, $fields = '*')
    {

        $obj_mager_role_mdl = D('manager_role');
        $result = array();
        if ($role_id)
        {
            if (is_array($role_id))
            {
                $where['role_id'] = array(
                                        'in', $role_id 
                );
                $result = $obj_mager_role_mdl->where($where)->field($fields)->select();
            }
            
            if (is_numeric($role_id))
            {
                $where['role_id'] = intval($role_id);
                $result = $obj_mager_role_mdl->where($where)->field($fields)->find();
            }
        }
        else
        {
            $result = $obj_mager_role_mdl->field($fields)->select();
        }
        
        return $result;
    }

    public function formatMangerInfo(&$mager_info)
    {

        if (! $mager_info)
        {return false;}
        // 获取角色信息
        $role_id = $mager_info['role_id'];
        if ($role_id == 0)
        {
            $mager_info['role_name'] = '超级管理员';
        }
        else
        {
            $role_info = $this->getRole($role_id, 'role_name');
            if ($role_info)
            {
                $mager_info['role_name'] = $role_info['role_name'];
            }
            else
            {
                $mager_info['role_name'] = '角色不存在';
            }
        }
        
        // 对密码解密
        $encode_pwd = $mager_info['passwd'];
        $this->decodePwd($encode_pwd, $mager_info['salt']);
        $mager_info['decode_pwd'] = $encode_pwd;
        
        return true;
    }

    protected function _formatList(&$manger_list)
    {

        if (! $manger_list)
        {return false;}
        
        $role_ids = array_column($manger_list, 'role_id');
        $role_list = $this->getRole($role_ids, 'role_id,role_name');
        $role_list = array_column($role_list, 'role_name', 'role_id');
        
        foreach ($manger_list as &$val)
        {
            if ($val['role_id'] == 0)
            {
                $val['role_name'] = '超級管理员';
            }
            else
            {
                $val['role_name'] = isset($role_list[$val['role_id']]) ? $role_list[$val['role_id']] : '未分配角色';
            }
            
            $encode_pwd = $val['passwd'];
            $this->decodePwd($encode_pwd, $val['salt']);
            $val['decode_pwd'] = $encode_pwd;
        }
        
        return true;
    }

}
 
 